// *********************************************
// * Raamatukogu  *
// *********************************************
// animations.lua - Loads animation control

function GM:CalcMainActivity( ply, velocity )
	// Define defaults
	local ideal = (ply.AggrStance and ACT_IDLE_ANGRY_MELEE) or ACT_IDLE
	local wep = ply:GetActiveWeapon()
	local SHOTGUN = false
	if (ValidEntity( wep )) then
		local class = wep:GetClass()
		if (class == "weapon_shotgun") then SHOTGUN = true end
	end
	local seq = -1
	if (SHOTGUN) then ideal = (ply.AggrStance and ACT_IDLE_SHOTGUN_AGITATED) or ACT_IDLE_SHOTGUN_RELAXED end
	
	// Determine speed
	local len2d = velocity:Length2D()
	local RUNNING = (len2d >= 180)
	local WALKING = (len2d >= 0.5)
	
	// Turn off aggressive stance
	if (RUNNING or WALKING) then ply.AggrStance = false end
	
	// Are we crouching?
	if (ply:Crouching()) then
		ideal = ACT_COVER_LOW
		if (WALKING) then ideal = ACT_WALK_CROUCH end
		if (RUNNING) then ideal = ACT_RUN_CROUCH end
		if (WALKING || RUNNING) && SHOTGUN then ideal = ACT_WALK_CROUCH_RIFLE end
	else
		if (WALKING) then ideal = ACT_WALK end
		if (RUNNING) then ideal = ACT_RUN end
		if (WALKING || RUNNING) && SHOTGUN then ideal = ACT_WALK_RIFLE_RELAXED end
	end
	
	// Are we playing an ACT or sequence?
	if (ply.A_Act && (CurTime() < ply.A_End)) then ideal = ply.A_Act end
	if (ply.S_Seq && (CurTime() < ply.S_End)) then seq = ply.S_Seq end
	
	// Return
	return ideal, seq
end

/*local Translate = {
	weapon_crowbar = {
		[ACT_IDLE] = ACT_IDLE_ANGRY_MELEE,
		//[ACT_WALK] = ACT
		//[ACT_PRIMA] = ACT_
	}
}*/
       
function GM:TranslateActivity( ply, act )
	/*local aggr = ply:GetAggr()
	if (!aggr) then	return act end
	local wep = ply:GetActiveWeapon()
	if (!ValidEntity( wep )) then return act end
	local class = wep:GetClass()
	local trans = Translate[ class ]
	if (!trans) then return act end
	return trans[ act ] or act*/
	return act
end

function GM:DoAnimationEvent( ply, event, data )
	if event == PLAYERANIMEVENT_ATTACK_PRIMARY then
		local wep = ply:GetActiveWeapon()
		if (!ValidEntity( wep )) then return end
		local class = wep:GetClass()
		if (SERVER) then
			if (class == "weapon_crowbar") || (wep.IsMelee) then
				ply:PlayAction( ACT_MELEE_ATTACK_SWING, 0.8 )
			end
			if (class == "weapon_shotgun") then
				ply:PlayAction( ACT_RANGE_ATTACK_SHOTGUN, 0.8 )
			end
		end
		ply.AggrStance = true
		return ACT_VM_PRIMARYATTACK
		   
	elseif event == PLAYERANIMEVENT_RELOAD then
   
		if ply:Crouching() then

			ply:AnimRestartGesture( GESTURE_SLOT_ATTACK_AND_RELOAD, ACT_MP_RELOAD_CROUCH )
		else
			ply:AnimRestartGesture( GESTURE_SLOT_ATTACK_AND_RELOAD, ACT_MP_RELOAD_STAND )
		end
	   
		return ACT_INVALID
		   
	elseif event == PLAYERANIMEVENT_JUMP then
   
		ply.m_bJumping = true
		ply.m_bFirstJumpFrame = true
		ply.m_flJumpStartTime = CurTime()
	   
		ply:AnimRestartMainSequence()
	   
		return ACT_INVALID
		   
	elseif event == PLAYERANIMEVENT_CANCEL_RELOAD then
   
		ply:AnimResetGestureSlot( GESTURE_SLOT_ATTACK_AND_RELOAD )
	   
		return ACT_INVALID
	end

	return nil
end

local metaPly = _R.Player

if (CLIENT) then

	local function umPlayAction( um )
		local ent = um:ReadEntity()
		local act = um:ReadShort()
		local time = um:ReadFloat()
		ent.A_Act = act
		ent.A_End = CurTime()+time
		ent:AnimRestartMainSequence()
	end
	usermessage.Hook( "play_action", umPlayAction )
	
	local function umPlaySequence( um )
		local ent = um:ReadEntity()
		local seq = um:ReadString()
		local time = um:ReadFloat()
		ent.S_Seq = ent:LookupSequence( seq )
		ent.S_End = CurTime()+time
		ent:AnimRestartMainSequence()
	end
	usermessage.Hook( "play_seq", umPlaySequence )

end

if (SERVER) then
	
	function metaPly:PlayAction( act, time )
		umsg.Start( "play_action" )
			umsg.Entity( self )
			umsg.Short( act )
			umsg.Float( time )
		umsg.End()
		self.A_Act = act
		self.A_End = CurTime()+time
		self:AnimRestartMainSequence()
	end
	
	function metaPly:PlaySequence( seq, time )
		umsg.Start( "play_seq" )
			umsg.Entity( self )
			umsg.String( seq )
			umsg.Float( time )
		umsg.End()
		self.S_Seq = self:LookupSequence( seq )
		self.S_End = CurTime()+time
		self:AnimRestartMainSequence()
	end
	
	function metaPly:PlayPickup( relativepos )
		local zdiff = relativepos.z - self:GetShootPos().z 
		if (zdiff <= -32) then
			self:PlayAction( ACT_PICKUP_GROUND, 1 )
		else
			self:PlayAction( ACT_PICKUP_RACK, 1 )
		end
	end

end